import { defineStore } from 'pinia'
import request from '../utils/axios'

export type Live = {
    hotLive: {
        name: string,
        img: string
    }[],
    hotActive: {
        img: string
    }[]
}
export type Game = {
    playbill: string,
    banner: {
        name: string,
        img: string
    }[],
    newgame: string[]
}
export type Manga = {
    playbill: {
        name: string,
        img: string
    }[],
    hotManga: string[]
}

export type PopCardData = {
    live: Live,
    game: Game,
    manga: Manga
}
export const useHeaderStore = defineStore('Header', {
    state: () => {
        return {
            data: <PopCardData>{}
        }
    },
    getters: {
        getLive: (store) => {
            return store.data.live
        },
        getGame: (store) => {
            return store.data.game
        },
        getManga: (store) => {
            return store.data.manga
        }
    },

    actions: {
        async getData(): Promise<PopCardData> {
            if (Object.keys(this.data).length !== 0) {
                return this.data
            }
            const data = await request.getHeaderData('')
            if (data.status == 200 && data.data.success) {
                this.data = data.data
            }
            return data.data
        }
    }
})

export type UserInfo = {
    name: string,
    img: string,
    lv: number,
    ex: number,
    coins: number,
    bCoins: number,
    sub: number,
    fans: number,
    trends: number
}
export const useUserStore = defineStore('User', {
    state: () => {
        return {
            userInfo: <UserInfo>{},
            requiredEx: <{
                [k: number]: number
            }>{
                    1: 200,
                    2: 1500,
                    3: 4500,
                    4: 10800,
                    5: 28800
                },
            token: 'asfasfdasdfasdfsadfa'
        }
    },
    getters: {
        getResidualEx: (store) => {
            return store.requiredEx[store.userInfo.lv] - store.userInfo.ex
        },
        getResidualExPe: (store) => {
            return store.userInfo.ex / store.requiredEx[store.userInfo.lv * 100]
        }
    },
    actions: {
        async getData() {
            const data = {
                name: 'TES2022输越南',
                img: '/src/img/face.jpg',
                lv: 5,
                ex: 28530,
                coins: 1185.8,
                bCoins: 0,
                sub: 36,
                fans: 1,
                trends: 147
            }
            this.userInfo = data
            this.token = 'xxxxxxxxxxxxxxx'
            return new Promise<UserInfo>((resolve, reject) => {
                resolve(data)
            })
        }
    }
})
export type RecommendCard = {
    img: string,
    title: string,
    tag: {
        text: string,
        type: number
    },
    state: number,
    playsNum: number,
    barrage: number,
    time: string,
    date: string,
    author: string
}
export const useRecommendStore = defineStore('Recommend', {
    state: () => {
        return {
            main: <RecommendCard[]>[],
            live: <RecommendCard[]>[]
        }
    },
    getters: {
        getMain: (store) => {
            return store.main
        },
        getLive: (store) => {
            return store.live
        }
    },
    actions: {
        async getData() {
            if (this.main.length > 0) {
                return
            }
            const data = await request.getRecommendData('')
            if (data.status === 200 && data.data.success) {
                this.main = data.data.main
                this.live = data.data.live
            }
        },
        async refreshLive() {
            const data = await request.getRecommendData('')//懒得单独写接口了
            if (data.status === 200 && data.data.success) {
                this.live = data.data.live
            }
        },
        async refreshMain() {
            const data = await request.getRecommendData('')//懒得单独写接口了
            if (data.status === 200 && data.data.success) {
                this.main = data.data.main
            }
        }
    }
})